<template>
  <div>
    <a-card :bordered="false" >
      <!-- 条件搜索 -->
<!--       <div class="table-page-search-wrapper">
        <a-form layout="inline">
          <a-row :gutter="48">
            <a-col :md="8" :sm="24">
              <a-form-item label="字段名">
                <a-input v-model="queryParam.key" placeholder="请输入字段名" allow-clear/>
              </a-form-item>
            </a-col>
            <a-col :md="8" :sm="12">
              <span class="table-page-search-submitButtons">
                <a-button type="primary" @click="handleQuery"><a-icon type="search" />查询</a-button>
                <a-button style="margin-left: 8px" @click="resetQuery"><a-icon type="redo" />重置</a-button>
              </span>
            </a-col>
          </a-row>
        </a-form>
      </div> -->
      <!-- 操作 -->
      <div class="table-operations">
        <a-button type="primary" @click="$refs.createForm.handleAdd()" v-hasPermi="['system:post:add']">
          <a-icon type="plus" />新增
        </a-button>
      </div>
      <!-- 增加修改 -->
      <create-form
        ref="createForm"
        :tableName="tableName"
        @ok="handleOk"
      />
      <!-- 数据展示 -->
      <a-table
        :loading="loading"
        rowKey="id"
        :columns="columns"
        :data-source="columnList"
        :pagination="false">
        <span slot="createTime" slot-scope="text, record">
          {{ parseTime(record.createTime) }}
        </span>
        <span slot="operation" slot-scope="text, record">
          <a @click="$refs.createForm.handleUpdate(record, undefined)" v-hasPermi="['system:post:edit']">
            <a-icon type="edit" />修改
          </a>
          <a-divider type="vertical" v-hasPermi="['system:post:remove']" />
          <a @click="handleDelete(record)" v-hasPermi="['system:post:remove']">
            <a-icon type="delete" />删除
          </a>
        </span>
      </a-table>
    </a-card>
  </div>
</template>

<script>

import { delColumn } from '@/api/free/column'
import CreateForm from './modules/CreateForm'

export default {
  name: 'FreeColumn',
  components: {
    CreateForm
  },
  props: {
    tableName: {
      type: String,
      required: true
    },
    columnList: {
      type: Array,
      required: true
    }
  },
  data () {
    return {
      selectedRowKeys: [],
      selectedRows: [],
      // 高级搜索 展开/关闭
      advanced: false,
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      ids: [],
      loading: false,
      total: 0,
      // 状态数据字典
      statusOptions: [],
      queryParam: {
        key: undefined,
        status: undefined
      },
      columns: [
        {
          title: '字段编码',
          dataIndex: 'code',
          ellipsis: true,
          align: 'center'
        },
        {
          title: '字段名称',
          dataIndex: 'name',
          ellipsis: true,
          align: 'center'
        },
        {
          title: '显示顺序',
          dataIndex: 'sort',
          align: 'center'
        },
        {
          title: '数据类型',
          dataIndex: 'htmlType',
          align: 'center'
        },
        {
          title: '长度限制',
          dataIndex: 'maxLength',
          align: 'center'
        },
        {
          title: '必填项',
          dataIndex: 'isRequired',
          align: 'center'
        },
        {
          title: '创建时间',
          dataIndex: 'createTime',
          ellipsis: true,
          scopedSlots: { customRender: 'createTime' },
          align: 'center'
        },
        {
          title: '操作',
          dataIndex: 'operation',
          width: '15%',
          scopedSlots: { customRender: 'operation' },
          align: 'center'
        }
      ]
    }
  },
  created () {
    this.getDicts('sys_normal_disable').then(response => {
      this.statusOptions = response
    })
  },
  methods: {
    // 字段状态字典翻译
    statusFormat (row) {
      return this.selectDictLabel(this.statusOptions, row.status)
    },
    /** 搜索按钮操作 */
    handleQuery () {
      this.$emit('reload')
    },
    /** 重置按钮操作 */
    resetQuery () {
      this.queryParam = {
        key: undefined,
        status: undefined
      }
      this.handleQuery()
    },
    /** 删除按钮操作 */
    handleDelete (row) {
      this.$confirm({
        title: '确认删除所选中数据?',
        content: `当前选中的字段为 ${row.name}(${row.code})`,
        onOk: () => {
          delColumn(row.id).then(res => {
            this.$emit('reload')
            this.$message.success(
                '删除成功',
                3
              )
          })
        },
        onCancel () {}
      })
    },
    /** 导出按钮操作 */
    handleExport () {
      this.$confirm({
        title: '是否确认导出?',
        content: '此操作将导出当前条件下所有数据而非选中数据',
        onOk: () => {},
        onCancel () {}
      })
    },
    handleOk() {
      this.$emit('reload')
    }
  }
}
</script>
